import sys

import qt_material
from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import QApplication, QWidget, QDialog, QFileDialog, QMessageBox

from All_code import deduce_window, function_window, main_window, my_sqlite


class MyMainWindow(QWidget, main_window.Ui_Form):
    def __init__(self):
        super(MyMainWindow, self).__init__()
        self.myfunction = None
        self.setupUi(self)
        self.signal_slot()

    def open_write_case(self):
        self.hide()
        self.myfunction = MyFunction()
        self.myfunction.tabWidget.setCurrentIndex(0)
        self.myfunction.show()

    def open_diagnose_case(self):
        self.hide()
        self.myfunction = MyFunction()
        self.myfunction.tabWidget.setCurrentIndex(1)
        self.myfunction.show()

    def open_treatment_scheme(self):
        self.hide()
        self.myfunction = MyFunction()
        self.myfunction.tabWidget.setCurrentIndex(2)
        self.myfunction.show()

    def open_treatment_record(self):
        self.hide()
        self.myfunction = MyFunction()
        self.myfunction.tabWidget.setCurrentIndex(3)
        self.myfunction.show()

    def signal_slot(self):
        self.pushButton.clicked.connect(self.open_write_case)
        self.pushButton_2.clicked.connect(self.open_diagnose_case)
        self.pushButton_3.clicked.connect(self.open_treatment_scheme)
        self.pushButton_4.clicked.connect(self.open_treatment_record)


class MyFunction(QWidget, function_window.Ui_Form):
    def __init__(self):
        super(MyFunction, self).__init__()
        self.setupUi(self)
        self.signal_slot()

    def show_area(self):
        region_name = self.comboBox_3.currentText()
        string_sql = f"select area_name from Area where region_name = '{region_name}'"
        result = my_sqlite.MySqlite().return_select_content(string_sql)
        items = []
        for i in result:
            items.append(i[0])
        self.comboBox_14.clear()
        self.comboBox_14.addItems(items)

    def show_time(self):
        mature_name = self.comboBox_7.currentText()
        string_sql = f"select seed_month from SeedTime where mature_name = '{mature_name}'"
        result = my_sqlite.MySqlite().return_select_content(string_sql)
        items = []
        for i in result:
            items.append(i[0])
        self.comboBox_5.clear()
        self.comboBox_5.addItems(items)

    def show_seed(self):
        variety_name = self.comboBox.currentText()
        string_sql = f"select seed_name from Seed where variety_name = '{variety_name}'"
        result = my_sqlite.MySqlite().return_select_content(string_sql)
        items = []
        for i in result:
            items.append(i[0])
        self.comboBox_4.clear()
        self.comboBox_4.addItems(items)

    def show_symptom(self):
        symptom_name = self.comboBox_2.currentText()
        string_sql = None
        if symptom_name == '':
            self.listWidget.clear()
        else:
            if symptom_name == '叶片染病':
                string_sql = f"select foliage from PotatoDiseaseBank"
            if symptom_name == '茎部染病':
                string_sql = f"select stem from PotatoDiseaseBank"
            if symptom_name == '植株状况':
                string_sql = f"select plant from PotatoDiseaseBank"
            if symptom_name == '块茎染病':
                string_sql = f"select tuber from PotatoDiseaseBank"
            if symptom_name == '叶柄染病':
                string_sql = f"select petiole from PotatoDiseaseBank"
            if symptom_name == '根部染病':
                string_sql = f"select root from PotatoDiseaseBank"
            result = my_sqlite.MySqlite().return_select_content(string_sql)
            items = []
            for i in result:
                if i[0] == '':
                    continue
                if i[0] is None:
                    continue
                items.append(i[0])
            self.listWidget.clear()
            self.listWidget.addItems(items)

    def set_case_picture(self):
        img_name, img_type = QFileDialog.getOpenFileName(self, '打开图片', "Qrc_resource", "*.jpg;;*.png;;All File(*)")
        # jpg = QtGui.QPixmap(img_name).scaled(self.label_12.width(), self.label_12.height())
        jpg = QtGui.QPixmap(img_name)
        self.label_12.setPixmap(jpg)

    def remove_case_picture(self):
        self.label_12.setPixmap(QtGui.QPixmap(''))

    def save_message(self):
        string_sql = "select message_number from PotatoMessageBank"
        result = my_sqlite.MySqlite().return_select_content(string_sql)
        my_list = []
        for i in result:
            my_list.append(i[0])
        if self.lineEdit_2.text() == '':
            QMessageBox.information(self, '插入错误', '病例号值不能为空', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
        elif self.lineEdit_2.text() not in my_list:
            string_sql = f"insert into PotatoMessageBank(message_number,area_name,seed_month,seed_method,seed_name,diagnose_time) values('{self.lineEdit_2.text()}','{self.comboBox_14.currentText()}','{self.comboBox_5.currentText()}','{self.comboBox_8.currentText()}','{self.comboBox_4.currentText()}','{self.dateEdit.text()}')"
            my_sqlite.MySqlite().commit_operation(string_sql)

        else:
            QMessageBox.warning(self, "插入错误", "病历号重复!", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)

    def show_disease_picture(self):
        self.lineEdit.setText("马铃薯环腐病")
        if self.lineEdit.text() != '':
            jpg_path = 'Qrc_resource/' + self.lineEdit.text() + '.jpeg'
            jpg = QtGui.QPixmap(jpg_path)
            self.label_14.setPixmap(jpg)
            string_sql = f"select feature,foliage,tuber,stem,petiole,seeding,root from PotatoCharacteristic where disease_name = '{self.lineEdit.text()}'"
            result = my_sqlite.MySqlite().return_select_content(string_sql)
            disease_list = []
            for j in result[0]:
                if j == '':
                    continue
                elif j is None:
                    continue
                else:
                    disease_list.append(j)
            try:
                self.textEdit_2.setText(disease_list[0])
                self.textEdit_3.setText(disease_list[1])
                self.textEdit_4.setText(disease_list[2])
                self.textEdit_5.setText(disease_list[3])
                self.textEdit_6.setText(disease_list[4])
            except Exception as e:
                print(e)
            string_sql = f"select plant_treatment,pesticide_treatment from PotatoTreatmentBank where disease_name = '{self.lineEdit.text()}'"
            result = my_sqlite.MySqlite().return_select_content(string_sql)
            self.textEdit_10.setText(result[0][0])
            self.textEdit_11.setText(result[0][1])
        else:
            print("疾病名称为空!")

    def signal_slot(self):
        self.comboBox_3.activated.connect(self.show_area)
        self.comboBox_7.activated.connect(self.show_time)
        self.comboBox.activated.connect(self.show_seed)
        self.comboBox_2.activated.connect(self.show_symptom)
        self.pushButton_2.clicked.connect(self.set_case_picture)
        self.pushButton_6.clicked.connect(self.remove_case_picture)
        self.pushButton.clicked.connect(self.save_message)
        self.show_disease_picture()


class MyDeduce(QDialog, deduce_window.Ui_Dialog):
    def __init__(self):
        super(MyDeduce, self).__init__()
        self.setupUi(self)
        self.signal_slot()

    def signal_slot(self):
        pass


class ShowWindow:
    def __init__(self, window_name=MyMainWindow):
        self.window_name = window_name
        self.show_window()

    def show_window(self):
        # 高清屏幕自适应
        QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
        app = QApplication(sys.argv)
        # 导入qdarkstyle样式
        # app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
        # 导入qt_material样式
        qt_material.apply_stylesheet(app, theme='dark_amber.xml', invert_secondary=True)
        win = self.window_name()
        win.show()
        sys.exit(app.exec_())


if __name__ == '__main__':
    ShowWindow()
